package com.wanmait.recycle.service.impl;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.dao.SolrDAO;
import com.wanmait.recycle.mapper.HistoryMapper;
import com.wanmait.recycle.mapper.RubbishMapper;
import com.wanmait.recycle.mapper.RubbishUnitMapper;
import com.wanmait.recycle.mapper.UnitMapper;
import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.pojo.RubbishUnit;
import com.wanmait.recycle.pojo.Secondtype;
import com.wanmait.recycle.pojo.Unit;
import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.ConfigService;
import com.wanmait.recycle.service.RubbishService;
import com.wanmait.recycle.util.PageContext;
import com.wanmait.recycle.util.Pager;

@Service
@Transactional
public class RubbishServiceImpl implements RubbishService {

	@Resource
	private RubbishMapper rubbishMapper; 
	@Resource
	private HistoryMapper historyMapper; 
	@Resource
	private RubbishUnitMapper rubbishUnitMapper; 
	@Resource
	private UnitMapper unitMapper;
	@Resource
	private SolrDAO solrDAO;
	@Resource
	private ConfigService configService;
	
	
	@Override
	public void insert(Rubbish object) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Rubbish object) {
		// TODO Auto-generated method stub
		rubbishMapper.update(object);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		rubbishMapper.deleteById(id);
		historyMapper.setUnEnableByTypeAndObjectId(1,id);
	}

	@Override
	public void deleteByIds(String ids) {
		// TODO Auto-generated method stub
		ids = ids.substring(0, ids.length()-1);
		String[] nn =ids.split(",");
		
		rubbishMapper.deleteByIds(nn);
		for(String n:nn) {
			Integer num=Integer.parseInt(n);
			historyMapper.setUnEnableByTypeAndObjectId(1,num);
		}
	}

	@Override
	public Rubbish findById(Integer id) {
		// TODO Auto-generated method stub
		return rubbishMapper.selectByPrimaryKey(id);
	} 
	
	@Override
	public PageInfo<Rubbish> findAllByPager() {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(), Integer.parseInt(config.get("后台垃圾分页").get("mRubbishPage")));
		List<Rubbish> rubbishs = rubbishMapper.findAll();
		PageInfo<Rubbish> pageInfo = new PageInfo<Rubbish>(rubbishs,7);
		return pageInfo;
	}
	
	@Override
	public int getDataCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Rubbish> findAll(Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<Unit> findAllUnits() {
		// TODO Auto-generated method stub
		return unitMapper.findAll();
	}
	@Override
	public List<Rubbish> findAll(Rubbish object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getDataCount(Rubbish object) {
		// TODO Auto-generated method stub
		return 0;
	}
	
		@Override
	public List<Rubbish> findAll(Pager pager, Rubbish object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insertById(Integer id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Rubbish> findAll() {
		// TODO Auto-generated method stub
		return rubbishMapper.findAll();
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(String ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void recallById(Integer id) {
		// TODO Auto-generated method stub
		rubbishMapper.recallById(id);
		historyMapper.setEnableByTypeAndObjectId(1,id);
	}
	@Override
	public PageInfo<Rubbish> findAllBySearch(String searchStr,String beginTime, String endTime) {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(),Integer.parseInt(config.get("后台垃圾分页").get("mRubbishPage")));
		List<Rubbish> rubbishs;
		if("可回收垃圾".equals(searchStr)||"可回收垃".equals(searchStr)||"可回收".equals(searchStr)||"可回".equals(searchStr)||"可".equals(searchStr)) {
			 rubbishs = rubbishMapper.findAllBySearch(searchStr, beginTime, endTime);
		}
		else {
			 rubbishs = rubbishMapper.findAllBySearchRecyclable(searchStr, beginTime, endTime);
		   }
		
		PageInfo<Rubbish> pageInfo = new PageInfo<Rubbish>(rubbishs);
		
		return pageInfo;
	}

	@Override
	public void addRubbish(String name, Integer type1Id, Integer type2id, String pic,String content,Admin admin,String unitString) {
		// TODO Auto-generated method stub
		
		
		Date date=new Date();
		Timestamp inputTime = new Timestamp(date.getTime());
		Rubbish object=new Rubbish();
		object.setName(name);
		Secondtype st=new Secondtype();
		st.setId(type2id);
		
		Firsttype ft=new Firsttype();
		ft.setId(type1Id);
		st.setFirsttype(ft);
		object.setSecondtype(st);
		object.setInputTime(inputTime);
		object.setCheckTime(inputTime);
		object.setContent(content);
		 
		
		object.setAdmin(admin);
		Userinfo userinfo=new Userinfo();
		userinfo.setId(0);
		object.setUserinfo(userinfo);
		object.setPic(pic);
		object.setSortCount(0);
		object.setEnable(true);
		object.setIsCheck(true);
		rubbishMapper.insertSelective(object);
		String[] nn=unitString.split(",");
		int rubbishMaxId=rubbishMapper.findMaxId();
		if( nn.length>=1) {
			for(int i=1;i<=nn.length-1;i++) {
				RubbishUnit rubbishUnit=new RubbishUnit();
				rubbishUnit.setRubbishId(rubbishMaxId);
				rubbishUnit.setUnitId(Integer.parseInt(nn[i]));
				rubbishUnitMapper.insert(rubbishUnit);
			}
		}
		else {
			
		}
	}

	@Override
	public Rubbish selectById(Integer id) {
		// TODO Auto-generated method stub
		return rubbishMapper.selectByPrimaryKey(id);
	}
	@Override
	public void updateById(Integer id,String name, Integer type1Id, Integer type2id, String pic,String content,Admin admin,String unitString) {
		// TODO Auto-generated method stub
		
		String[] nn=unitString.split(",");
		
		rubbishUnitMapper.deleteAllById(id);
		if( nn.length>=1) {
			for(int i=1;i<=nn.length-1;i++) {
				RubbishUnit rubbishUnit=new RubbishUnit();
				rubbishUnit.setRubbishId(id);
				rubbishUnit.setUnitId(Integer.parseInt(nn[i]));
				rubbishUnitMapper.insert(rubbishUnit);
			}
		}
		else {
			
		}
		
		
		Rubbish object=new Rubbish();
		object.setId(id);
		object.setName(name);
		Secondtype st;
		if(type2id!=null) {
			st=new Secondtype();
			st.setId(type2id);
			Firsttype ft=new Firsttype();
			ft.setId(type1Id);
			st.setFirsttype(ft);
		}
		else {
			st=null;
		}
	
		object.setSecondtype(st);
		object.setContent(content);	
		object.setAdmin(admin);
		Userinfo userinfo=new Userinfo();
		userinfo.setId(0);
		object.setUserinfo(userinfo);
		if(pic==null||"".equals(pic)||"null".equals(pic)) {
			pic=rubbishMapper.selectByPrimaryKey(id).getPic();
			
		}
		
		object.setPic(pic);
		
		
		rubbishMapper.updateByPrimaryKeySelective(object);
	}

	@Override
	public List<Rubbish> findByUserId(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void userAddRubbish(String objectName, Integer firsttypeId, Integer secondtypeId, String pic, String content,
			Userinfo userinfo,String unitString) {
		String[] nn=unitString.split(",");
		
		Date date=new Date();
		Timestamp inputTime = new Timestamp(date.getTime());
		Rubbish object=new Rubbish();
		object.setName(objectName);
		Secondtype st=new Secondtype();
		st.setId(secondtypeId);
		
		Firsttype ft=new Firsttype();
		ft.setId(firsttypeId);
		st.setFirsttype(ft);
		object.setSecondtype(st);
		object.setInputTime(inputTime);
		
		object.setContent(content);
		 
		
		
		object.setUserinfo(userinfo);
		object.setPic(pic);
		object.setSortCount(0);
		object.setEnable(false);
		object.setIsCheck(false);
		rubbishMapper.insertSelective(object);
		int rubbishMaxId=rubbishMapper.findMaxId();
		if( nn.length>=1) {
			for(int i=1;i<=nn.length-1;i++) {
				RubbishUnit rubbishUnit=new RubbishUnit();
				rubbishUnit.setRubbishId(rubbishMaxId);
				rubbishUnit.setUnitId(Integer.parseInt(nn[i]));
				rubbishUnitMapper.insert(rubbishUnit);
			}
		}
		else {
			
		}
		
	}

	@Override
	public Integer findMaxId() {
		// TODO Auto-generated method stub
		return rubbishMapper.findMaxId();
	}

	@Override
	public void realDelete(Integer objectId) {
		// TODO Auto-generated method stub
		rubbishMapper.deleteByPrimaryKey(objectId);
	}
	
	

//根据二级分类找垃圾 3.7 ad 
	@Override
	public List<Rubbish> findAllBySecondTypeId(Integer id) {
		// TODO Auto-generated method stub
		return rubbishMapper.findAllBySecondTypeId(id);
	}

	@Override
	public List<Rubbish> findAllBySearch(String seachData) {
		// TODO Auto-generated method stub
		return rubbishMapper.findBySearch(seachData);
	}

	@Override
	public List<Rubbish> seachBySolr(String keyword, Pager pager) {
		pager.setPageSize(5);
		return solrDAO.findRubbishPage(keyword, pager);
	}

	@Override
	public Rubbish findByRubbishNameAndContent(String objectName, String content) {
		// TODO Auto-generated method stub
		return rubbishMapper.findByRubbishNameAndContent(objectName,  content);
	}
	@Override
	public PageInfo<Rubbish> findAllUnCheckByPager() {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(), Integer.parseInt(config.get("后台垃圾分页").get("mRubbishPage")));
		List<Rubbish> rubbishs = rubbishMapper.findAllUnCheck();
		PageInfo<Rubbish> pageInfo = new PageInfo<Rubbish>(rubbishs,7);
		return pageInfo;
	}
	@Override
	public PageInfo<Rubbish> findAllPassByPager() {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(), Integer.parseInt(config.get("后台垃圾分页").get("mRubbishPage")));
		List<Rubbish> rubbishs = rubbishMapper.findAllPass();
		PageInfo<Rubbish> pageInfo = new PageInfo<Rubbish>(rubbishs,7);
		return pageInfo;
	}

	@Override
	public PageInfo<Rubbish> findAllUnPassByPager() {
		// TODO Auto-generated method stub
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(), Integer.parseInt(config.get("后台垃圾分页").get("mRubbishPage")));
		List<Rubbish> rubbishs = rubbishMapper.findAllUnPass();
		PageInfo<Rubbish> pageInfo = new PageInfo<Rubbish>(rubbishs,7);
		return pageInfo;
	}

	@Override
	public Boolean judgeIfRepetition(String objectName, Integer nSecondTypeId) {
		// TODO Auto-generated method stub
		Rubbish rubbish=rubbishMapper.findByNameAndSecondTypeId(objectName,nSecondTypeId);
		if(rubbish==null) {
			return false;
		}
		return true;
	}
	@Override
	public PageInfo<Rubbish> findAllByPrimarykey(Integer nScId) {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(),Integer.parseInt(config.get("后台第二类型分页").get("mSecondPage")));
		List<Rubbish> rbs;
		rbs=rubbishMapper.findAllBySecondTypeId(nScId);
		
		PageInfo<Rubbish> pageInfo = new PageInfo<Rubbish>(rbs);
		
		return pageInfo;
	}
}
